package com.pb.camera.work;


import android.util.Log;
import android.widget.Toast;

import com.pb.camera.h264.VedioPlayView;
import com.tutk.IOTC.AVAPIs;
import com.tutk.IOTC.IOTCAPIs;

public class IOTCInit {

    protected String mUIdV;
    private VedioPlayView mDependView;
    private int mAvIndex;
    private int mSid;
    private static IOTCInit aDefault = new IOTCInit();

    public IOTCInit(String uIdv) {
        this.mUIdV = uIdv;
    }

    private IOTCInit() {
    }

    public static IOTCInit getDefault() {
        return aDefault;
    }


    public void stopClient() {
        AVAPIs.avClientStop(mAvIndex);
        System.out.printf("avClientStop OK\n");
        IOTCAPIs.IOTC_Session_Close(mSid);
        System.out.printf("IOTC_Session_Close OK\n");
        AVAPIs.avDeInitialize();
        IOTCAPIs.IOTC_DeInitialize();
        System.out.printf("StreamClient exit...\n");
    }

    public boolean runInit() {
//    	        System.out.println("StreamClient start...");

        // use which Master base on location, port 0 means to get a random port

//    	        int ret = IOTCAPIs.IOTC_Initialize(0, "m1.iotcplatform.com",
//    	                "m2.iotcplatform.com", "m4.iotcplatform.com",
//    	                "m5.iotcplatform.com");
        int port = (int) (10000 + (System.currentTimeMillis() % 10000));
        int ret = IOTCAPIs.IOTC_Initialize(port, "50.19.254.134", "122.248.234.207",
                "m4.iotcplatform.com", "m5.iotcplatform.com");
        System.out.printf("IOTC_Initialize() ret = %d\n", ret);
        if (ret != IOTCAPIs.IOTC_ER_NoERROR) {
            System.out.printf("IOTCAPIs_Device exit...!!\n");
            return false;
        }

        // alloc 3 sessions for video and two-way audio
        AVAPIs.avInitialize(3);
        Log.e("初始化成功", "i+iii" + mUIdV);
        mSid = IOTCAPIs.IOTC_Connect_ByUID(mUIdV);
        if (mSid == IOTCAPIs.IOTC_ER_NETWORK_UNREACHABLE);

        System.out.printf("Step 2: call IOTC_Connect_ByUID(%s).......\n", mUIdV);
        Log.d("初始化:", "sid:" + mSid);
        int[] srvType = new int[1];
        mAvIndex = AVAPIs.avClientStart(mSid, "admin", "888888", 20000, srvType, 0);
        Log.e("av初始化结果", "i+iii" + mAvIndex);
        System.out.printf("Step 2: call avClientStart(%d).......\n", mAvIndex);

        if (mAvIndex < 0) {
            System.out.printf("avClientStart failed[%d]\n", mAvIndex);
            return false;
        }

        if (startIpcamStream(mAvIndex)) {
            if (mInitSuccessCallBack != null)
                mInitSuccessCallBack.OnInitSuccess(mUIdV, mAvIndex);
        }
        return true;
    }

    private boolean startIpcamStream(int avIndex) {
        AVAPIs av = new AVAPIs();
        int ret = av.avSendIOCtrl(avIndex, AVAPIs.IOTYPE_INNER_SND_DATA_DELAY,
                new byte[2], 2);
        if (ret < 0) {
            System.out.printf("start_ipcam_stream failed[%d]\n", ret);
            return false;
        }

        // This IOTYPE constant and its corrsponsing data structure is defined in
        // Sample/Linux/Sample_AVAPIs/AVIOCTRLDEFs.h
        //
        int IOTYPE_USER_IPCAM_START = 0x1FF;
        ret = av.avSendIOCtrl(avIndex, IOTYPE_USER_IPCAM_START,
                new byte[8], 8);
        if (ret < 0) {
            System.out.printf("start_ipcam_stream failed[%d]\n", ret);
            return false;
        }

        int IOTYPE_USER_IPCAM_AUDIOSTART = 0x300;
        ret = av.avSendIOCtrl(avIndex, IOTYPE_USER_IPCAM_AUDIOSTART,
                new byte[8], 8);
        if (ret < 0) {
            System.out.printf("start_ipcam_stream failed[%d]\n", ret);
            return false;
        }
        return true;
    }

    private InitSuccessCallBack mInitSuccessCallBack;

    public void setUID(String UID) {
        this.mUIdV = UID;
    }

    public interface InitSuccessCallBack {
        public void OnInitSuccess(String mUIdV, int avIndex);
    }

    public void setOnInitSuccessCallBack(InitSuccessCallBack initSuccessCallBack) {
        this.mInitSuccessCallBack = initSuccessCallBack;
    }
}
